package com.sale.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.system.domain.SysFields;
import com.sale.system.domain.SysSettle;
import com.sale.system.mapper.SysSettleMapper;
import com.sale.system.service.ISysSettleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description 结账反结账
 * @Param Settle
 * @Date 23-07-07
 * @Author hl
 **/
@RequiredArgsConstructor
@Service
public class SysSettleServiceImpl implements ISysSettleService {

    private final SysSettleMapper sysSettleMapper;

    /**
     * 根据条件分页查询结账反结账信息
     *
     * @param sysSettle
     * @return TableDataInfo
     */
    @Override
    public TableDataInfo<SysSettle> selectSettleList(SysSettle sysSettle, PageQuery pageQuery) {
        Page<SysSettle> page = sysSettleMapper.selectPageSettleList(pageQuery.build(), this.buildQueryWrapper(sysSettle));
        return TableDataInfo.build(page);
    }

    /**
     * 查询集
     *
     * @param settle
     * @return Wrapper<SysSettle>
     */
    private Wrapper<SysSettle> buildQueryWrapper(SysSettle settle) {
        Map<String, Object> params = settle.getParams();
        QueryWrapper<SysSettle> wrapper = Wrappers.query();
        wrapper
            .eq(ObjectUtil.isNotNull(settle.getCreateBy()), "create_by", settle.getCreateBy())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
